//
//  RequestSession.m
//  YAMainProject
//
//  Created by 郑逸凯 on 2018/11/2.
//  Copyright © 2018年 郑逸凯. All rights reserved.
//

#import "RequestSession.h"
#import <AFNetworking/AFNetworking.h>

@interface RequestSession ()

@property (nonatomic,strong)AFHTTPSessionManager *jsonRequestManager;

@end
@implementation RequestSession

+ (AFHTTPSessionManager*) sessionManager {
    static dispatch_once_t onceToken;
    static AFHTTPSessionManager *manager = nil;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain",nil];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.requestSerializer.timeoutInterval = WO_REQUEST_TIMEINTERVAL;
    });
    
    return manager;
}

- (AFHTTPSessionManager*)jsonRequestManager {
    if (!_jsonRequestManager) {
       AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain",nil];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.requestSerializer.timeoutInterval = WO_REQUEST_TIMEINTERVAL;
        _jsonRequestManager = manager;
    }
    return _jsonRequestManager;
}

#pragma mark -- GET请求 --

- (void)getWithURLString:(NSString *)URLString parameters:(id)parameters completionHandler:(void (^)(id , NSError * ))completionHandler {
    AFHTTPSessionManager *manager = self.isJsonRequest ?self.jsonRequestManager :[RequestSession sessionManager];
    [manager GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError *error  = [NetworkHelper woResponseSuccess:responseObject];
        //如果成功的话，error 为nil
        if (completionHandler) {
            completionHandler(responseObject,error);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (completionHandler) {
            completionHandler(nil,[NSError errorWithDomain:@"网络请求失败，请稍后再试" code:-1 userInfo:nil]);
        }
    }];
}


#pragma mark -- POST请求 --

- (void)postWithURLString:(NSString *)URLString parameters:(id)parameters completionHandler:(void (^)(id, NSError *))completionHandler {
    AFHTTPSessionManager *manager = self.isJsonRequest ? self.jsonRequestManager :[RequestSession sessionManager];
    [manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError *error  = [NetworkHelper woResponseSuccess:responseObject];
        //如果成功的话，error 为nil
        if (completionHandler) {
            completionHandler(responseObject,error);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (completionHandler) {
            completionHandler(nil,[NSError errorWithDomain:@"网络请求失败，请稍后再试" code:-1 userInfo:nil]);
        }
    }];
}




@end
